import axios from 'axios'
import { getItem, removeItem } from '@/utils/token'
import { Message } from 'element-ui'
import router from '@/router'
const request = axios.create({
  baseURL: "/api",
})


// 请求拦截器
request.interceptors.request.use(function(config) {
config.headers.Authorization=`Bearer ${getItem("token")}`
  return config;
})
// 响应拦截器
request.interceptors.response.use(function(config) {
  const { status, message } = config.data
  if (status == '500'){
    Message.error('登录已过期，请重新登录')
    // removeItem('token')
    router.push('/login')
  }else if(status !== '200' && status !== '500'){
    Message.error(message)
  }
  return config
},error =>{
  return Message.error(error)
})
export default request
